﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using com.iss.inventory.Entity;

namespace com.iss.inventory.Controllers
{
    public class DelegationController
    {
        public DelegationController() { }

        public String getRoleByName(String empName) {
            InvManagementEntities IME = new InvManagementEntities();
            Employee e = IME.Employees.First<Employee>(x => x.Name == empName);
            return e.Role;
        }

        public String getHeapTypeByName(String empName)
        {
            InvManagementEntities IME = new InvManagementEntities();
            Employee e = IME.Employees.First<Employee>(x => x.Name == empName);
            if (e.DeptID == 310002) return "Manager";
            else return "Head";
        }

        public String[] getDelegateByName(String empName) {
            InvManagementEntities IME = new InvManagementEntities();
            Employee e = IME.Employees.First<Employee>(x => x.Name == empName);
            DelegateDetail d=null;Employee dtor;
            DateTime now=DateTime.Now;
            try
            {
                var dModel = from de in IME.DelegateDetails
                             where (de.DateTill > now) && (de.DelegateID==e.EmployeeID)
                             select de;
                foreach (DelegateDetail dd in dModel) {
                    d = dd;
                }
                dtor=IME.Employees.First<Employee>(z=>z.EmployeeID==d.DelegatorID);
            }
            catch (Exception ex) { return null; }
            String role="";
            if (dtor.Role == "Head")
            {
                if (dtor.DeptID == 310002) role = "Manager";
                else role = "Head";
            }
            else role = dtor.Role;
            String[] dele = { role, dtor.Name, ((DateTime)d.DateFrom).ToShortDateString(), ((DateTime)d.DateTill).ToShortDateString() };
            return dele;
        }

        public String[] getDelegatorByName(String empName)
        {
            InvManagementEntities IME = new InvManagementEntities();
            Employee er = IME.Employees.First<Employee>(x => x.Name == empName);
            String role = "";
            if (er.Role == "Head")
            {
                if (er.DeptID == 310002) role = "Manager";
                else role = "Head";
            }
            else role = er.Role;
            DelegateDetail d=null; Employee ee; DateTime now = DateTime.Now;
            try
            {
                var dModel = from dr in IME.DelegateDetails
                            where (dr.DateTill > now) && (dr.DelegatorID == er.EmployeeID)
                            select dr;
                foreach (DelegateDetail dd in dModel)
                {
                    if (dd.DateTill > now) {
                        d = dd;
                        break;
                    }
                }
                ee = IME.Employees.First<Employee>(z => z.EmployeeID == d.DelegateID);
            }
            catch (Exception ex) {
                String[] den = { role, "", "", "" };
                return den;
            }
            String[] de = { role, ee.Name, ((DateTime)d.DateFrom).ToShortDateString(), ((DateTime)d.DateTill).ToShortDateString() };
            return de;
        }

        public String SubmitDelegate(String empName, String txtDelegateEmp, String txtDelegateFrom, String txtDelegateTill)
        {
            InvManagementEntities IME = new InvManagementEntities();
            Employee ee;
            try
            {
                ee = IME.Employees.First<Employee>(x => x.Name == txtDelegateEmp);
            }
            catch(Exception ex) {
                return "This employee does not exist";
            }
            if (ee.DeptID != IME.Employees.First<Employee>(x => x.Name == empName).DeptID) {
                return "This person does not belong to your department";
            }
            if (getDelegateByName(ee.Name) != null)
            {
                return "This person has already been delegated by someone else";
            }
            else {
                Employee er = IME.Employees.First<Employee>(x => x.Name == empName);
                DelegateDetail dd = new DelegateDetail();
                dd.DelegateID = ee.EmployeeID; dd.DateFrom = Convert.ToDateTime(txtDelegateFrom); dd.DateTill = Convert.ToDateTime(txtDelegateTill); dd.DelegatorID = er.EmployeeID;
                IME.AddToDelegateDetails(dd);
                IME.SaveChanges();
                return "Delegate Successfully";
            }
        }

        public String CancelDelegate(String empName) {
            InvManagementEntities IME = new InvManagementEntities();
            Employee er = IME.Employees.First<Employee>(x => x.Name == empName);
            DelegateDetail d = null;
            var dModel = from de in IME.DelegateDetails
                         where (de.DateTill > DateTime.Now) && (de.DelegatorID == er.EmployeeID)
                         select de;
            foreach (DelegateDetail dd in dModel)
            {
                d = dd;
            }
            d.DateFrom = ((DateTime)d.DateFrom).AddYears(-1); d.DateTill = ((DateTime)d.DateTill).AddYears(-1);
            IME.SaveChanges();
            return "Cancel Delegate Successfully";
        }


    }
}
